<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class FeedController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");
	protected $typeArr = array(0=>'公告',1=> '消息');
	
    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
	
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
		
			$map['feedback'] = array('LIKE','%'.$sh_q.'%');
			$map['reply'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}


		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
		$list = M('shop_feedback')->where($where)->order('id desc, add_time desc')->page($pageno.','.$pageCount)->select();
		foreach( $list as $key => $item){
		  $list[$key]['detail_s_id'] = M('customer')->where('cid="'.$item['cid'].'"')->getField('id');
		  $list[$key]['fullname'] = M('customer_detail')->where('c_id="'.$list[$key]['detail_s_id'].'"')->getField('fullname');
		}
               // print_r($list);exit;
                $this->assign('list',$list);
		$count = M('shop_feedback')->where($where)->count();
		$this->assign('count',$count);	
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->display('Feed/index');
    }
	
 public function editStatus(){
	     $cid = I('cid');
             $status = I('state');
	     $dataInfo = M('shop_feedback')->find($cid);
	     if( $dataInfo ){
	      if( $status == 1){
		     $data['status'] = 0;
		   }else{
		      $data['status'] = 1;
		   }
		   $result = M('shop_feedback')->where('id="'.$dataInfo['id'].'"')->save($data);
	     }
	     if( $result ){
		 $info = 100;
		 $this->ajaxReturn($info);
		 }
	 
	 }
	 
    public  function edit(){
          $id = I('id');
	  $dataInfo = M('shop_feedback')->where('id="'.$id.'"')->find($id);
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		 $this->display();
	  }
    }
	
     public  function  saveEdit(){
          	 $data['reply'] = I('reply');
	         $id = I('updateid');
                 
		 if( !empty(I('reply')) ){
		     $data['reply_status'] = 1;
		  }else{
		     $data['reply_status'] = 0;
		  }
                if($data){
		      $result = M('shop_feedback')->where('id="'.$id.'"')->save($data);
		 }
		 if($result){
		    $info['status'] =100;
		    $info['info'] = '修改成功';
		    $this->ajaxReturn($info);
		 }else{
		    $info['status'] =100;
		    $this->ajaxReturn($info);
		 }
       
	    

  }	
  
  
     public function add(){
		 $typeArr  = array(0=>'公告',1=>'消息');
		 $this->assign('typeArr', $typeArr);
		 $this->display();
    }
	
	
	public  function save(){
	         $data['title']= I('title');
                 $data['type_id'] = I('colid');
                 $data['content'] = I('content');
		 $data['author'] = $_SESSION['admin_user_id'];
	        if( $data ){
                    $result = M('shop_feedback')->add($data);		 
		 }
	       if( $result ){
		 $info['status'] = 100;
		 $info['info'] = '添加成功';
		 $this->ajaxReturn($info);
		 
		 }
  }
      public function del(){
           $id = $_REQUEST['id'];
	   if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
	    $list = M('shop_feedback')->where($where)->delete();
	    $this->redirect('Feed/index');
    }

	
	 
}